<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { todoList } from "@/api/workflow/todo";
import { myProcessList, stopProcess } from "@/api/workflow/process";
import {
  finishedList
} from "@/api/workflow/finished";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      todoNum: 0,
      myProcNum: 0,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        category: null,
      },
      // 查询参数
      myProcessQueryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        category: null,
        key: null,
        tenantId: null,
        deployTime: null,
        derivedFrom: null,
        derivedFromRoot: null,
        parentDeploymentId: null,
        engineVersion: null,
        finishTime: null,
      },

    }
  },
  created() {
    this.queryParams.partyIdTo = this.$store.state.user.partyId;
    // 调用轮询
    this.polling();
  },
  mounted() {
    this.$nextTick(() => {
      todoList(this.queryParams).then((response) => {
        this.todoNum = response.total;
        myProcessList(this.myProcessQueryParams).then((response) => {
          this.myProcNum = response.total;
          // console.info("myProcNum 1 :" + this.myProcNum)
          this.initChart(this.todoNum, this.myProcNum)
        });
      });
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  methods: {

    // 轮询方法 /** 查询待办 */
    polling(params) {
      todoList(this.queryParams).then((response) => {
        myProcessList(this.myProcessQueryParams).then((response) => {
          this.myProcNum = response.total;
        });
        this.todoNum = response.total;
        console.info("todo num :" + this.todoNum)
        this.initChart(this.todoNum, this.myProcNum)
        this.pollingST = setTimeout(() => {
          clearTimeout(this.pollingST);
          this.polling(params);
        }, 50000);
      });
    },

    initChart(todoNum, myProcNum) {
      console.info("myProc Num :" + myProcNum)

      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        title: {
          text: '   ' + todoNum + '条 \r\n 待审批',
          left: 'center',
          top: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          // top: '5%',
          bottom: '1%',
          left: 'center',
          // x: 'center', //可设定图例在左、右、居中
          // y: 'bottom', //可设定图例在上、下、居中
        },

        series: [
          {
            name: '待办审批',
            type: 'pie',
            radius: ['40%', '60%'],

            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: false,
                fontSize: '20',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: todoNum, name: '待审批' },
              { value: myProcNum, name: '我的申请' },
            ]
          }
        ]
      })

      this.chart.on("click", (param) => {
        console.info("param:" + param);
        // param.dataIndex 获取当前点击索引，
        console.info("dataIndex:" + param.dataIndex);
        if (param.dataIndex == 0) {
          // 任务待办
          var _path = "/workflow/todo";
          this.$router.push({
            path: _path,
          });
        }
        if (param.dataIndex == 1) {
          // 我的任务
          var _path = "/workflow/process";
          this.$router.push({
            path: _path,
          });
        }

      });
    },
  }
}
</script>

<style>
.chart {
  top: -50px
}
</style>
